//
//  BGComposeViewController.swift
//  WeiboSwift
//
//  Created by 战场原黑仪 on 16/7/28.
//  Copyright © 2016年 战场原黑仪. All rights reserved.
//

import UIKit

class BGComposeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupNav()
        setupUI()
        setupLayout()
    }

    //MARK: - 设置视图
    private func setupUI() {
        view.backgroundColor = UIColor.whiteColor()
        view.addSubview(composeTextView)
        view.addSubview(composePictureView)
        view.addSubview(composeToolbarView)
    }
    
    private func setupLayout() {
        // 01
        composeTextView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view).offset(UIEdgeInsetsZero)
        }
        // 02
        composePictureView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: SCREEN_W - 20, height: SCREEN_W - 20))
            make.top.equalTo(composeTextView).offset(200)
            make.centerX.equalTo(composeTextView)
        }
        // 03
        composeToolbarView.snp_makeConstraints { (make) -> Void in
            make.height.equalTo(44)
            make.leading.trailing.bottom.equalTo(view)
        }

    }
    
    private func setupNav() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", imageName: nil, target: self, action: #selector(self.cancelAciton))
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发布", imageName: nil, target: self, action: #selector(self.sendAciton))
        //设置标题
//        navigationItem.rightBarButtonItem?.enabled = false
        navigationItem.titleView = titleView
    }
    
    // MARK: - 懒加载控件
    //标题视图
    private lazy var titleView: UILabel = {
        // 用户名字
        let name = BGUserAccountViewModel.sharedTools.userAccount?.screen_name ?? ""
        let str = "发微博\n\(name)"
        // 获取range
        let range = (str as NSString).rangeOfString(name)
        // 创建一个可变的富文本
        let attr = NSMutableAttributedString(string: str)
        // 设置富文本
        attr.addAttributes([NSForegroundColorAttributeName: ThemeColor,NSFontAttributeName: UIFont.systemFontOfSize(12)], range: range)
        let lab = UILabel()
        lab.numberOfLines = 0
        lab.textAlignment = .Center
        lab.attributedText = attr
        lab.sizeToFit()
        return lab
    }()
    //文字
    private lazy var composeTextView: BGComposeTextView = {
        let textView = BGComposeTextView()
        textView.font = Medium_Font
        textView.placeholder = "简单点,说话的方式简单点"
        
        return textView
    }()
    // 配图
    private lazy var composePictureView: BGComposePictureView = BGComposePictureView()
    // 底部视图
    private lazy var composeToolbarView: BGComposeToolBar = BGComposeToolBar()
}

//MARK: - 监听事件
extension BGComposeViewController {
    //取消
    @objc private func cancelAciton() {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    // 发布
    @objc private func sendAciton(){
        let message = UIAlertController(title: "发布微博", message: "直接发布", preferredStyle: .Alert)
        let cancel = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
//        let enter = UIAlertAction(title: "确定", style: .Default) { (action) in
//            self.dismissViewControllerAnimated(true, completion: nil)
//        }
        
        message.addAction(cancel)
        
        self.presentViewController(message, animated: true, completion: nil)
    }
}